package org.jeecgframework.web.controller;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.service.ZySnCheckRecordServiceI;
import org.jeecgframework.web.system.pojo.base.ZySnCheckRecordEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Title: Controller
 * @Description: 序列号验证异常记录
 * @author onlineGenerator
 * @date 2016-05-27 11:26:32
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/zySnCheckRecordController")
public class ZySnCheckRecordController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ZySnCheckRecordController.class);

	@Autowired
	private ZySnCheckRecordServiceI zySnCheckRecordService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 序列号验证异常记录列表 页面跳转
	 * @return
	 */
	@RequestMapping(params = "zySnCheckRecord")
	public ModelAndView zySnCheckRecord(HttpServletRequest request) {
		return new ModelAndView("com/zybk/zy_sn_check_record/zySnCheckRecordList");
	}

	/**
	 * easyui AJAX请求数据
	 */
	@RequestMapping(params = "datagrid")
	public void datagrid(ZySnCheckRecordEntity zySnCheckRecord,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(ZySnCheckRecordEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, zySnCheckRecord, request.getParameterMap());
        boolean flag = false; //页面查询条件为空时不做查询
		Map<String, String[]> parameterMap = request.getParameterMap();
        for (String key : parameterMap.keySet()) {
            if (key.equals("dealResult") || key.equals("exceptionSource") || key.equals("bp") ||
            		key.equals("sn") || key.equals("exceptionDate_begin") || key.equals("exceptionDate_end")) {
                if (!StringUtil.isEmpty(parameterMap.get(key)[0])) {
                    flag = true;//页面 查询条件为空不做查询
                    break;
                }
            }
        }
        if (!flag) {
        	cq.eq("dealResult", StaticValue.DIC_SN_CHECK_DEAL_RESULT_TODO);
        } 
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.zySnCheckRecordService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * @author xiul
	 * 2016-5-27 下午2:06:18
	 * Description 在处理 或者 忽略操作之前检查所选项目是否可以操作 
	 * @param ids ids
	 */
	@ResponseBody
	@RequestMapping(params="checkSnCheckRecord")
	public AjaxJson checkSnCheckRecord(@RequestParam(value="ids",defaultValue="")String ids){
		AjaxJson j = new AjaxJson();
		try {
			String[] idsArr = ids.split(",");
			for (String id : idsArr) {
				ZySnCheckRecordEntity zySnCheckRecord = systemService.get(ZySnCheckRecordEntity.class, id);
				if(!zySnCheckRecord.getDealResult().equals(StaticValue.DIC_SN_CHECK_DEAL_RESULT_TODO)){
					j.setMsg("存在不能操作的记录");
					j.setSuccess(false);
					return j;
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			message = "验证失败";
			j.setSuccess(false);
		}
		return j;
		
	}
	
	/**
	 * @author xiul
	 * 2016-5-27 下午2:07:27
	 * Description 处理记录 或者 忽略记录 
	 * @param type 操作类型 deal ignore
	 * @param ids ids
	 */
	@ResponseBody
	@RequestMapping(params="doHandleSnCheckRecord")
	public AjaxJson doHandleSnCheckRecord(
			@RequestParam(value="type")String type,
			@RequestParam(value="ids")String ids){
		AjaxJson j = new AjaxJson();
		try {
			String operation = "";
			if(type.equals("deal")){
				operation = StaticValue.DIC_SN_CHECK_DEAL_RESULT_DONE;
			}else if(type.equals("ignore")){
				operation = StaticValue.DIC_SN_CHECK_DEAL_RESULT_IGNORE;
			}
			String[] idsArr = ids.split(",");
			for (String id : idsArr) {
				ZySnCheckRecordEntity zySnCheckRecord = systemService.get(ZySnCheckRecordEntity.class, id);
				zySnCheckRecord.setDealResult(operation);
				systemService.saveOrUpdate(zySnCheckRecord);
			}
			j.setMsg("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			message = "保存失败";
		}
		return j;
		
	}
	
}
